<?php
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Di\FactoryDefault;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Transaction\Manager as TransactionManager;

/**
 * The FactoryDefault Dependency Injector automatically registers the right services to provide a full stack framework
 */
$di = new FactoryDefault();

//注册路由
$di->set('router', function () {
    $router = new Router(false);
//添加路由


$router->add("/", array(
                'module' => 'frontend',
                'namespace' => 'Apps\Front\Controllers',
                'controller' => 'index',
                'action' => 'index',
        ));
        
        $router->add("/:controller", array(
                'module' => 'frontend',
                'namespace' => 'Apps\Front\Controllers',
                'controller' => 'index',
                'action' => 'index',
        ));

        // $router->add("/index/", array(
        //        'module' => 'frontend',
        //         'namespace' => 'Apps\Front\Controllers',
        //         'controller' => 'index',
        //         'action' => 'index',
        // ));

        
        $router->add("/:controller", array(
                'module' => 'frontend',
                'namespace' => 'Apps\Front\Controllers',
                'controller' => 1,
                'action' =>'index'
        ));

        $router->add("/:controller/", array(
                'module' => 'frontend',
                'namespace' => 'Apps\Front\Controllers',
                'controller' => 1,
                'action' =>'index'
        ));

        $router->add("/:controller/:action", array(
                'module' => 'frontend',
                'namespace' => 'Apps\Front\Controllers',
                'controller' => 1,
                'action' => 2,
        ));
        $router->add("/:controller/:action/:params", array(
                'module' => 'frontend',
                'namespace' => 'Apps\Front\Controllers',
                'controller' => 1,
                'action' => 2,
                'params' => 3
        ));

        /*
         begin routers of back module
         author: xu
        */
        $router->add("/admin", array(
                'module' => 'backend',
                'namespace' => 'Apps\Back\Controllers',
                'controller' => 'index',
                'action' => 'index',
        ));

        $router->add("/admin/", array(
               'module' => 'backend',
                'namespace' => 'Apps\Back\Controllers',
                'controller' => 'index',
                'action' => 'index',
        ));
            

        $router->add("/admin/:controller", array(
                'module' => 'backend',
                'namespace' => 'Apps\Back\Controllers',
                'controller' =>1,
                'action' =>'index',
        ));

        $router->add("/admin/:controller/:action", array(
            'module' => 'backend',
            'namespace' => 'Apps\Back\Controllers',
            'controller' => 1,
            'action' => 2,
        ));

        $router->add("/admin/:controller/:action/", array(
            'module' => 'backend',
            'namespace' => 'Apps\Back\Controllers',
            'controller' => 1,
            'action' => 2,
        ));

        $router->add("/admin/:controller/:action/:params", array(
            'module' => 'backend',
            'namespace' => 'Apps\Back\Controllers',
            'controller' => 1,
            'action' => 2,
            'params'=>3
        ));
             
    return $router;
});


//设置基本URL参数
$di->set('url', function () {
    $url = new UrlResolver();
    $url->setBaseUri('/daigou/');

    return $url;
});

//设置事务
 $di->setShared('transactions', function () {
    return new TransactionManager();
 });


/**
 * Setting up the view component
 */
$di->setShared('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new VoltEngine($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            return $volt;
        },
        '.html' => 'Phalcon\Mvc\View\Engine\Php'
    ));

    return $view;
});

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db', function () use ($config) {
    return new DbAdapter($config->database->toArray());
});

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});

/**
 * Starts the session the first time some component requests the session service
 */
$di->setShared('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});

/**
* Set the default namespace for dispatcher
*/
$di->setShared('dispatcher', function() use ($di) {
	$dispatcher = new Phalcon\Mvc\Dispatcher();
	//$dispatcher->setDefaultNamespace('Apps\Front\Controllers');

	return $dispatcher;
});
